package com.shelljor.mianshi;

import android.databinding.DataBindingUtil;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.shelljor.mianshi.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity implements Callback<BaseEntity> {
       private ActivityMainBinding mBinding;
       private CommentRecyclerAdapter<Object>mAdapter;

    //http://trainddapi.51ts.cn/common/findPubobjectList.do?appkey=peixunduoduo&timestamp=1488497313787&sign=2ea70a01a9f3efc4eb4f8050ab4bc704&opttype=17

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        mBinding=DataBindingUtil.setContentView(this, R.layout.activity_main);
        Map<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(null, Pair.create(R.layout.item, BR.item));

        mAdapter = new CommentRecyclerAdapter<>(this, new ArrayList<>(), map);
        mBinding.mainRecyclerview.setAdapter(mAdapter);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://trainddapi.51ts.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IService iService = retrofit.create(IService.class);
        iService.getJson().enqueue(this);


    }

    @Override
    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
        Log.d("flag", response.body().getData().getRows().get(0).toString());
        List<RowsEntity> rows = response.body().getData().getRows();

        mAdapter.addAll(rows);

    }

    @Override
    public void onFailure(Call<BaseEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, "error"+t.getMessage(), Toast.LENGTH_SHORT).show();

    }
}
